<?php

include "map-config.php";

$default = "email address";
$defaultText = "email address";

if($_GET['email']) {
	$email = htmlspecialchars($_GET['email']);
	$defaultText = htmlentities($email);
}



// if we've all these variables it's an ajax request
if($_GET['email'] AND $_GET['lat'] AND $_GET['lng'] AND $_GET['s']) {
	@mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 
	@mysql_select_db(DB_NAME);

	
	// set the starting data
	$startLat = $_GET['lat'];
	$startlng = $_GET['lng'];
	
	// did we get an email variable?
	if($_GET['email']) {
		add_address($_GET['email'], $_GET['lng'], $_GET['lat']);
	}
	
	die(); // ajax query, so quit output
}

// set mapbuttontext to default if not set
if(!$mapButtonText) { 
	$mapButtonText = "save my location"; 
}

// do we want the email input
if($showEmailInput OR !$email) { 
	$inputEmail = '<input style="color: #aaa;" type="text" value="' . $defaultText . '" onfocus="this.value=\'\'; this.style.color = \''. INPUTCOLOR .'\';" id="mapemail" name="mapemail" />';
	$clearInput = 'document.getElementById("mapemail").style.color = "' . INPUTCOLOR . '";
document.getElementById("mapemail").value = "' . $default . '"; ';
}
else { // if not, pass url var 'email' through in a hidden input
	$hiddenInputEmail = '<input type="hidden" value="' . $_GET['email'] .'" name="mapemail" id="mapemail"/>';
}

?>

<form name="" method="post" action="" id="">
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo GMAPS_API_KEY ?>" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var map;
function getLocation(){
	script = document.createElement('script')
	script.src = "http://ipinfodb.com/ip_query.php?output=json&callback=load";
	document.body.appendChild(script)
}

function load(geoDict) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.enableScrollWheelZoom();
		map.addControl(new GLargeMapControl());
		GEvent.addListener(map, "click", clickFunc);
		var lat = 0; 
		var lng = 0;
		var centre = new GLatLng(geoDict['Latitude'], geoDict['Longitude']);
		map.setCenter(centre, 5);
		map.addOverlay(new GMarker(centre));
	}
}

function clickFunc(marker, point) {   
	document.getElementById("lat").value = point.lat().toFixed(5);
	document.getElementById("lng").value = point.lng().toFixed(5);
	map.clearOverlays();
	map.addOverlay(new GMarker(point));
}//]]>
</script>
<div id="mappy-email">
<div id="map" style="width: 480px; height: 350px; position: relative; background-color: rgb(229, 227, 223);"></div>
<input type="hidden" id="lat" name="lat" style="width: 80px;" type="text">
<input type="hidden" id="lng" name="lng" style="width: 80px;" type="text">
<?php echo $hiddenInputEmail; ?>

<script type="text/javascript">
//<![CDATA[
getLocation();
//]]>
</script>

<p>
<?php echo $inputEmail; ?>
&nbsp;<button onclick='
if(document.getElementById("mapemail").value == "" || document.getElementById("mapemail").value == "<?php echo $default ?>") { alert("Please enter your email address."); }
else {
<?php echo JQUERY; ?>.ajax({
   type: "GET",
   url: "<?php echo MAP_SCRIPT_LOCATION ?>map-selector.php",
   data: "email=" + document.getElementById("mapemail").value + "&lat=" + document.getElementById("lat").value + "&lng=" + document.getElementById("lng").value + "&s=1",
   success: function(msg){     
     alert( "Thank you, your location has been recorded!");
	<?php echo $clearInput; ?>
   }
 });} return false;' /><?php echo $mapButtonText; ?></button>
</p>
</form>
</div>
<?php

// add an address, longitude and latitude to the database table
function add_address($email, $longitude, $latitude) {

	$email = mysql_escape_string($email);
	$longitude = mysql_escape_string($longitude);
	$latitude = mysql_escape_string($latitude);
	$IP = $_SERVER['REMOTE_ADDR'];

	$query = mysql_query("INSERT INTO " . DB_TABLE . " (email, IP, longitude, latitude, datetime) VALUES ('$email', '$IP', '$longitude', '$latitude', NOW())") or die(mysql_error());
	$added = mysql_affected_rows();
	
	if($added > 0) {
		return true;
	}
	else {
		return false;
	}
}

?>